package com.alu4r.atcrowdfunding.manager.controller;


import com.alu4r.atcrowdfunding.bean.Cert;
import com.alu4r.atcrowdfunding.manager.service.CertService;
import com.alu4r.atcrowdfunding.manager.service.CertTypeService;
import com.alu4r.atcrowdfunding.util.JsonMessageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/certType")
public class CertTypeController {


    @Autowired
    CertTypeService certTypeService;
    @Autowired
    CertService certService;

    //点击复选框了添加用户类型许可
    @ResponseBody
    @RequestMapping("/deleteAcctTypeCert")
    public JsonMessageResult deleteAcctTypeCert(Integer certid, String accttype) {
        JsonMessageResult jsonMessageResult = null;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("certid", certid);
        paramMap.put("accttype", accttype);

        try {
            certTypeService.deleteAcctTypeCert(paramMap);
            jsonMessageResult = JsonMessageResult.success();
        } catch (Exception e) {
            jsonMessageResult = JsonMessageResult.fail();
            e.printStackTrace();
        }

        return jsonMessageResult;
    }

    //点击复选框了添加用户类型许可
    @ResponseBody
    @RequestMapping("/insertAcctTypeCert")
    public JsonMessageResult insertAcctTypeCert(Integer certid, String accttype) {
        JsonMessageResult jsonMessageResult = null;
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("certid", certid);
        paramMap.put("accttype", accttype);

        try {
            certTypeService.insertAcctTypeCert(paramMap);
            jsonMessageResult = JsonMessageResult.success();
        } catch (Exception e) {
            jsonMessageResult = JsonMessageResult.fail();
            e.printStackTrace();
        }

        return jsonMessageResult;
    }

    @RequestMapping("/toIndex")
    public String toIndex(Map<String, Object> map) {
        List<Cert> certs = certService.queryAllCert();
        map.put("certs", certs);
        //查询之前分配过的资质
        List<Map<String, Object>> certAcctTypeList = certTypeService.queryCertAcctType();
        map.put("certAcctTypeList", certAcctTypeList);
        return "certType/index";
    }

}
